import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Demo2 {
    public static void main(String[] args) throws SQLException {
        // 1. 创建数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java109?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("cjc123456");

        // 2. 建立连接
        Connection connection = dataSource.getConnection();

        // 3. 构造删除sql语句
        String sql = "delete from student where name = '张三'";
        PreparedStatement statement = connection.prepareStatement(sql);

        // 4. 执行 sql 语句
        statement.executeUpdate();

        // 5. 关闭资源,后获取的资源先关闭
        statement.close();
        connection.close();
    }
}
